package com.system.borrow.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.system.borrow.Constant.URLDefind;
import com.system.borrow.R;
import com.system.borrow.adapter.LoanList2Adapter;
import com.system.borrow.bean.LoanList;
import com.system.borrow.cache.CacheMode;
import com.system.borrow.callback.DialogCallback;
import com.system.borrow.http.utils.OkHttpUtils;
import com.system.borrow.utils.SecondTitle;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import okhttp3.Request;
import okhttp3.Response;

/**
 * 贷款
 */
public class LoanActivity extends BaseActivity {
    ListView lv_data;
    LoanList2Adapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loan);
        SecondTitle title = new SecondTitle(this);
        title.setTitle("贷款推荐",null);
        initView();
        addAction();
        loadData();
        initListener();
    }

    private void initListener() {
    }

    private void loadData() {
        OkHttpUtils.get(URLDefind.LOAN_TUIJIAN)
                .tag(this)
                .cacheMode(CacheMode.FIRST_CACHE_THEN_REQUEST)
                .execute(new DialogCallback<String>(this,String.class,"加载中...") {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        if(s!=null){
                            try {
                                JSONObject json = new JSONObject(s);
                                int statusCode = json.getInt("statusCode");
                                if(statusCode==200){
                                    Gson gson = new Gson();
                                    JSONArray data = json.getJSONArray("data");
                                    TypeToken<List<LoanList>> token = new TypeToken<List<LoanList>>(){};
                                    List<LoanList> tuiJianList = gson.fromJson(data.toString(),token.getType());
                                    adapter.setList(tuiJianList);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onCacheResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        if(s!=null){
                            try {
                                JSONObject json = new JSONObject(s);
                                int statusCode = json.getInt("statusCode");
                                if(statusCode==200){
                                    Gson gson = new Gson();
                                    JSONArray data = json.getJSONArray("data");
                                    TypeToken<List<LoanList>> token = new TypeToken<List<LoanList>>(){};
                                    List<LoanList> tuiJianList = gson.fromJson(data.toString(),token.getType());
                                    adapter.setList(tuiJianList);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
    }

    private void addAction() {
        adapter = new LoanList2Adapter(this);
        lv_data.setAdapter(adapter);
    }

    private void initView() {
        lv_data = (ListView) findViewById(R.id.lv_data);
    }
}
